package com.rfid.emergencywarehouse.presenter;

import com.rfid.base.base.BasePresenter;
import com.rfid.emergencywarehouse.manager.UserInfoManager;
import com.rfid.emergencywarehouse.view.ILoginView;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.LoginRes;
import com.rfid.emergencywarehouse.net.entity.UserInfoRes;

public class LoginPresenter extends BasePresenter<ILoginView> {


    public void login(String name,String pwd) {
        mView.onShowLoading("正在登录");
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    LoginRes res = NetApi.INSTANCE.login(name,pwd);
                    if (res.getData() != null) {
                        UserInfoRes info = NetApi.INSTANCE.getLoginUserInfo();
                        String name = "";
                        if (info.getData() != null) {
                            name = info.getData().getUserName();
                            UserInfoManager.INSTANCE.setName(name);
                            UserInfoManager.INSTANCE.setUserId(info.getData().getUserId());
                        }
                        String finalName = name;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mView != null) {
                                    mView.onHideLoading();
                                    mView.onLoginSuccess(finalName);
                                }
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mView != null) {
                                    mView.onHideLoading();
                                    mView.onLoginFail(res.getMsg());
                                }
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onHideLoading();
                                mView.onLoginFail("登录失败");
                            }
                        }
                    });
                }
            }
        });
    }
}
